package com.example.his.api.mis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.R;
import com.example.his.api.db.pojo.SystemEntiy;
import com.example.his.api.mis.controller.form.system.DeleteSystemByIdsForm;
import com.example.his.api.mis.controller.form.system.InsertSystemForm;
import com.example.his.api.mis.controller.form.system.UpdateSystemForm;
import com.example.his.api.mis.service.SystemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController("MisSystemController")
@RequestMapping(("/mis/system"))
public class SystemController {
  @Resource
  private SystemService systemService;
  
  @PostMapping("/searchAll")
  @SaCheckLogin
  public R searchAll() {
    ArrayList <SystemEntiy> list = systemService.searchAll();
    return R.ok().put("result", list);
  }
  
  @PostMapping("/insert")
  @SaCheckPermission(value = { "ROOT", "SYSTEM:INSERT" }, mode = SaMode.OR)
  public R insert(@RequestBody @Valid InsertSystemForm form) {
    Map param = BeanUtil.beanToMap(form);
    Integer id = systemService.insert(param);
    return R.ok().put("result", id);
  }
  
  @PostMapping("/deleteByIds")
  @SaCheckPermission(value = { "ROOT", "SYSTEM:DELETE" }, mode = SaMode.OR)
  public R deleteByIds(@RequestBody @Valid DeleteSystemByIdsForm form) {
    int rows = systemService.deleteByIds(form.getIds());
    return R.ok().put("result", rows);
  }
  
  @PostMapping("/update")
  @SaCheckPermission(value = { "ROOT", "SYSTEM:UPDATE" }, mode = SaMode.OR)
  public R update(@RequestBody @Valid UpdateSystemForm form) {
    Map param = BeanUtil.beanToMap(form);
    boolean b = systemService.update(param);
    return R.ok().put("result", b);
  }
  
  @GetMapping("/searchById/{id}")
  @SaCheckPermission(value = { "ROOT", "SYSTEM:SELECT" }, mode = SaMode.OR)
  public R searchById(@PathVariable("id") Integer id) {
    HashMap map = systemService.searchById(id);
    return R.ok().put("result", map);
  }
  
}
